package com.banciyuan.bcywebview.biz.main.mainpage.timeline;

import android.content.Context;
import android.text.TextUtils;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.banciyuan.bcywebview.base.helper.data.UserDataHelper;
import com.banciyuan.bcywebview.base.util.BaseObserver;
import com.banciyuan.bcywebview.biz.setting.MainSettingHelper;
import com.banciyuan.bcywebview.utils.http.CommonApiUtils;
import com.banciyuan.bcywebview.utils.http.HttpUtils;
import com.banciyuan.bcywebview.utils.http.StatusCodeUtils;
import com.banciyuan.bcywebview.utils.http.VolleyQueue;
import com.google.gson.Gson;

import org.json.JSONObject;

import de.greenrobot.daoexample.model.AppSetting;
import de.greenrobot.daoexample.model.NotificationData;

/**
 * Created by hy on 2015/7/22.
 */
public class TimeLineGroundHelper {

    private Context mContext;
    private RequestQueue mQueue;

    public TimeLineGroundHelper(Context mContext) {
        this.mContext = mContext;
        mQueue = VolleyQueue.getRquest(mContext);
    }


    public void getTimelineData(String since, final TimelineListener timelineListener) {
        Response.Listener<String> onSuccess = new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    if (StatusCodeUtils.dealStatusCode(response, mContext)) {
                        JSONObject temp = jsonObject.getJSONObject(HttpUtils.UPLOAD_DATA);
                        timelineListener.Succ(temp.getJSONArray(HttpUtils.UPLOAD_DATA), temp.getJSONObject("page"), jsonObject.getInt(HttpUtils.UPLOAD_STATUS));
                    } else {
                        timelineListener.fail("fail", jsonObject.getInt(HttpUtils.UPLOAD_STATUS));
                    }
                } catch (Exception e) {
                    timelineListener.fail("fail", 9999);
                }
            }
        };

        Response.ErrorListener onError = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                timelineListener.fail("fail", 9999);
            }
        };
        CommonApiUtils.getNewTimeLine(mContext, mQueue, onSuccess, onError, UserDataHelper.getInstance(mContext).getToken(), since);
    }

    public void getTimelineFirstData(String since, String score, final TimelineListener timelineListener) {
        if (!TextUtils.isEmpty(UserDataHelper.getInstance(mContext).getToken())) {
            getMessageData();
            getMainSettings();
        }
        Response.Listener<String> onSuccess = new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    if (StatusCodeUtils.dealStatusCode(response, mContext)) {
                        JSONObject temp = jsonObject.getJSONObject(HttpUtils.UPLOAD_DATA);
                        timelineListener.Succ(temp.getJSONArray(HttpUtils.UPLOAD_DATA), temp.getJSONObject("page"), jsonObject.getInt(HttpUtils.UPLOAD_STATUS));
                    } else {
                        timelineListener.fail("fail", jsonObject.getInt(HttpUtils.UPLOAD_STATUS));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    timelineListener.fail("fail", 9999);
                }
            }
        };

        Response.ErrorListener onError = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                timelineListener.fail("fail", 9999);
            }
        };
        CommonApiUtils.getFirstTimeLine(mContext, mQueue, onSuccess, onError, UserDataHelper.getInstance(mContext).getToken(), since, score);
    }

    public void getMessageData() {
        Response.Listener<String> onSuccess = new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    if (StatusCodeUtils.dealStatusCode(response, mContext)) {
                        String data = new JSONObject(response).getString(HttpUtils.UPLOAD_DATA);
                        Gson gson = new Gson();
                        NotificationData notification = gson.fromJson(data, NotificationData.class);
                        BaseObserver.getInstance().notifyWatchers(BaseObserver.MESSAGE_EVENT, notification);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };

        Response.ErrorListener onError = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
            }
        };
        CommonApiUtils.getMessageData(mContext, mQueue, onSuccess, onError);
    }

    public void getMainSettings() {
        if (!TextUtils.isEmpty(UserDataHelper.getInstance(mContext).getToken())) {
            MainSettingHelper.getMainSettings(mContext, new MainSettingHelper.MainSettingListener() {
                @Override
                public void getMainSettingSuccess(AppSetting appSetting) {
                    BaseObserver.getInstance().notifyWatchers(BaseObserver.MAINSETTING_EVENT, "false", appSetting);
                }

                @Override
                public void setMainSettingSuccess(String response) {
                }

                @Override
                public void onFail() {
                }
            });
        }
    }
}
